package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCatMapper;
import com.sky.service.ShoppingCatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCatServicelmpl implements ShoppingCatService {


    @Autowired
    private ShoppingCatMapper shoppingCatMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
     *   添加购物车
     *    @param shoppingCartDTO
     *
     * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到的购物车的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCatMapper.list(shoppingCart);

        //如果存在则更新数量

        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCatMapper.updateNumberById(shoppingCart1);
        }

        //如果不存在则插入新的购物车商品
        else {
            //判断本次添加到购物车的商品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //本次添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCatMapper.insert(shoppingCart);
        }



    }

    /**
     * 查看购物车
     */

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCatMapper.list(shoppingCart);
    }

    //根据用户ID来清空购物车
    @Override
    public void deleteById() {
        Long userId = BaseContext.getCurrentId();

        shoppingCatMapper.deleteByIds(userId);
    }


    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //设置查询条件，查询当前登录用户的购物车数据
        List<ShoppingCart> list = shoppingCatMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            Integer number = shoppingCart1.getNumber();

            if(number == 1)
            {
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCatMapper.deleteById(shoppingCart1.getId());

            }
            else
            {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart1.setNumber(number - 1);
                shoppingCatMapper.updateNumberById(shoppingCart1);
            }
        }

    }
}
